layui.use(['form','table', 'admin', 'ax', 'func','formSelects'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;

    /**
     * 阶段管理
     */
    var Stage = {
        tableId: "stageTable"
    };

    getSpecialtyType();

    getDepts();

    //获取专业类型并展示
    function getSpecialtyType() {
        var ajax = new $ax(Feng.ctxPath + "/specialtyType/showList");
        var result = ajax.start();
        $('#specialtyId').append('<option value="">所学专业</option>');
        result.data.map(item =>{
            $('#specialtyId').append('<option value="'+item.id+'">'+item.typeName+'</option>');
        });
        form.render('select');
    }

    //获取授课部门
    function getDepts() {
        var ajax = new $ax(Feng.ctxPath + "/dept/getDeptTeach");
        var result = ajax.start();
        $('#department').append('<option value="">请选择授课部门</option>');
        result.data.map(item =>{
            $('#department').append('<option value="'+item.id+'">'+item.name+'</option>');
        });
        form.render('select');
    }

    /**
     * 初始化表格的列
     */
    Stage.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'typeName', sort: true, title: '专业类型'},
            {field: 'chapters', sort: true, title: '章节'},
            {field: 'source', sort: true, title: '授课对象'},
            {field: 'deptName', sort: true, title: '授课部门'},
            {field: 'status', sort: true, title: '状态',templet(d){
                    return d.status==1?'启用':'禁用';
                }},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Stage.search = function () {
        var queryData = {};

        queryData['specialtyId'] = $('#specialtyId').val();
        queryData['chapters'] = $('#chapters').val();
        queryData['source'] = $('#source').val();
        queryData['department'] = $('#department').val();

        table.reload(Stage.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Stage.openAddDlg = function () {
        func.open({
            title: '添加阶段',
            content: Feng.ctxPath + '/stage/add',
            tableId: Stage.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      Stage.openEditDlg = function (data) {
          func.open({
              title: '修改阶段',
              content: Feng.ctxPath + '/stage/edit?id=' + data.id,
              tableId: Stage.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    Stage.exportExcel = function () {
        var checkRows = table.checkStatus(Stage.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Stage.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/stage/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Stage.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Stage.tableId,
        url: Feng.ctxPath + '/stage/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Stage.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Stage.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    Stage.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        Stage.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Stage.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Stage.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Stage.onDeleteItem(data);
        }
    });
});
